﻿/*----------------------------------------------------------------
//Copyright (C) 2012 赵国伟
//文件功能描述：收货地址管理客户端脚本
//创建人：赵国伟
//生成时间：2012-08-18 20:24:19
//----------------------------------------------------------------*/
var AjaxServerUrl = "/Home/AS/AjaxHomeCommon.aspx";
var ModelOfUserAddress;
var SelectOptionOfProvince = "";
var UserAddressID = 0;

///初始化
function Init() {
    UserAddressID = 0;
}

//执行删除
function Delete(id) {
    if (!confirm("系统提示：您确定要删除该条收货地址信息？"))
        return;
    AjaxGet(AjaxServerUrl + "?Action=DeleteUserAddress&UserAddressID=" + id);
}

///检查表单；
function CheckForm() {
    if ($("#txtReceiverName").val() == "") {
        alert("系统提示：请输入收货人！");
        $("#txtReceiverName").focus();
        return false;
    }
    if ($("#ddlProvince").val() == "") {
        alert("系统提示：请选择所在省份！");
        $("#ddlProvince").focus();
        return false;
    }
    if ($("#ddlCity").val() == "") {
        alert("系统提示：请选择所在城市！");
        $("#ddlCity").focus();
        return false;
    }
    if ($("#ddlArea").val() == "") {
        alert("系统提示：请选择所在区域！");
        $("#ddlArea").focus();
        return false;
    }
    if ($("#txtReceiverAddress").val() == "") {
        alert("系统提示：请输入详细地址！");
        $("#txtReceiverAddress").focus();
        return false;
    }
    if ($("#txtReceiverMobile").val() == "" && $("#txtReceiverPhone").val() =="") {
        alert("系统提示：手机号码与固定电话必须填一项！！");
        $("#txtReceiverMobile").focus();
        return false;
    }
    return true;
}

//保存；
function Save() {
    $("#btnSave").attr("disabled", true);
    if (!CheckForm()) {
        $("#btnSave").attr("disabled", false);
        return;
    }

    var data = clone(ModelOfUserAddress);
    data = { "_areaid": 0, "_createdate": "\/Date(1345293181481+0800)\/", "_isdefault": false, "_lastmodifydate": "\/Date(1345293181481+0800)\/", "_receiveraddress": null, "_receivermobile": null, "_receivername": null, "_receiverphone": null, "_receiverzipcode": null, "_useraddressid": 0, "_userid": 0 };
    data._useraddressid = UserAddressID;
    data._userid = 0;
    data._receivername = $("#txtReceiverName").val();
    data._areaid = $("#ddlArea").val();
    data._receiveraddress = $("#txtReceiverAddress").val();
    data._receivermobile = $("#txtReceiverMobile").val();
    data._receiverphone = $("#txtReceiverPhone").val();
    data._receiverzipcode = $("#txtReceiverZipCode").val();
    data._isdefault = $("#chkIsDefault").attr("checked");
    data._createdate = "/Date(" + CurrentDateStringToUTC() + ")/";
    data._lastmodifydate = "/Date(" + CurrentDateStringToUTC() + ")/";
    var url = AjaxServerUrl + "?Action=SaveUserAddress";
    AjaxPost(url, "btnSave", data);  //更新
}

//绑定收货地址信息到录入表单中；
function BindUserAddressToForm() {
    var url = AjaxServerUrl + "?action=GetUserAddress&UserAddressID=" + UserAddressID + "&tttt=" + Math.random();
    $.ajax(
    {
        type: "Get",
        url: url,
        dataType: "json",
        success: function (data) {
            if (data != null) {
                $("#txtReceiverName").val(data.AddressInfo._receivername);
                $("#ddlProvince").val(data.Area.Province._id);
                QueryCities(data.Area.Province._id, 'ddlCity', data.Area.Area._cityid);
                QueryAreas(data.Area.Area._cityid, 'ddlArea', data.AddressInfo._areaid);
                $("#txtReceiverAddress").val(data.AddressInfo._receiveraddress);
                $("#txtReceiverMobile").val(data.AddressInfo._receivermobile);
                $("#txtReceiverPhone").val(data.AddressInfo._receiverphone);
                $("#txtReceiverZipCode").val(data.AddressInfo._receiverzipcode);
                $("#chkIsDefault").attr("checked", data.AddressInfo._isdefault);
            }
        }
    });
}

///添加界面；
function Add() {
    Init();
    GetPopupDiv(600, 600, "#e2e2e2", "添加收货地址", GetHtmlForm());
}

//编辑界面；
function Edit(id) {
    Init();
    UserAddressID = id;
    GetPopupDiv(600, 600, "#e2e2e2", "编辑收货地址", GetHtmlForm());
    BindUserAddressToForm();
}

///获取录入界面的HTML;
function GetHtmlForm() {
    var html = new Array();
    var html = new Array();
    html.push("<form>");
    html.push("<table width=\"600\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"table\">");
    html.push("  <tr>");
    html.push("    <td colspan=\"2\" class=\"HeadTitle\">&gt;&gt;收货地址</td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">收货人：</td>");
    html.push("    <td><input name=\"txtReceiverName\" type=\"text\" id=\"txtReceiverName\" size=\"25\" class=\"border6\"/>");
    html.push("      <span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td width=\"103\" align=\"right\">所在地区：</td>");
    html.push("    <td><select name=\"ddlProvince\" id=\"ddlProvince\" onchange=\"QueryCities(this.options[this.selectedIndex].value,'ddlCity');\">");
    html.push("      <option value=\"\">-省份-</option>");
    html.push(SelectOptionOfProvince);
    html.push("    </select>");
    html.push("      <select name=\"ddlCity\" id=\"ddlCity\" onchange=\"QueryAreas(this.options[this.selectedIndex].value,'ddlArea');\">");
    html.push("        <option value=\"\">-城市-</option>");
    html.push("      </select>");
    html.push("      <select name=\"ddlArea\" id=\"ddlArea\">");
    html.push("        <option value=\"\">-区域-</option>");
    html.push("      </select></td>");
    html.push("    </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">详细地址：</td>");
    html.push("    <td ><span id=\"spArea\"></span><input name=\"txtReceiverAddress\" type=\"text\" id=\"txtReceiverAddress\" size=\"40\" />");
    html.push("      <span class=\"red\">(*)</span></td>");
    html.push("    </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\"> 手机号码：</td>");
    html.push("    <td><input name=\"txtReceiverMobile\" type=\"text\" id=\"txtReceiverMobile\" size=\"25\" />");
    html.push("      (手机与电话必须填一项)</td>");
    html.push("    </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\"> 电话号码：</td>");
    html.push("    <td><input name=\"txtReceiverPhone\" type=\"text\" id=\"txtReceiverPhone\" size=\"25\" /></td>");
    html.push("    </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\"> 邮政编码：</td>");
    html.push("    <td><input name=\"txtReceiverZipCode\" type=\"text\" id=\"txtReceiverZipCode\" size=\"25\" c/></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">&nbsp;</td>");
    html.push("    <td><label for=\"chkIsDefault\"><input name=\"chkIsDefault\" type=\"checkbox\" id=\"chkIsDefault\" checked=\"checked\" />");
    html.push("      是否设为默认</label></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td colspan=\"2\" align=\"center\"><input name=\"btnSave\" type=\"button\" id=\"btnSave\" value=\"  保 存  \" class=\"btn2\" onclick=\"Save();\">");
    html.push("      <input name=\"btnClose\" type=\"button\" id=\"btnClose\" value=\"  关闭本窗口  \" class=\"btn2\" onclick=\"yesOther(false);\"></td>");
    html.push("    </tr>");
    html.push("</table>");
    html.push("</form>");

    return html.join("");
}













